import { DrawToolAction, DrawToolActionTypes, DrawToolTypes } from "../actions/draw-tool.action";

export interface DrawTool {
    type: DrawToolTypes,
    enabled: boolean,
    active: boolean
}

export interface BufferOptions {
    unit: string,
    value: number,
    clone: boolean
}

export interface InputMarkOptions {
    lng: number;
    lat: number;
}

export interface DrawToolState {
    entities: { [key: string]: DrawTool },
    measureAvailable: boolean;
    mutex: DrawToolTypes[],
    buffer: BufferOptions,
    inputMark: InputMarkOptions
}

export const initialDrawToolState: DrawToolState = {
    entities: {
        Select: {
            type: DrawToolTypes.SELECT,
            enabled: true,
            active: false
        },
        Point: {
            type: DrawToolTypes.POINT,
            enabled: true,
            active: false
        },
        LineString: {
            type: DrawToolTypes.LINESTRING,
            enabled: true,
            active: false
        },
        Circle: {
            type: DrawToolTypes.CIRCLE,
            enabled: true,
            active: false
        },
        Rectangle: {
            type: DrawToolTypes.RECTANGLE,
            enabled: true,
            active: false
        },
        Polygon: {
            type: DrawToolTypes.POLYGON,
            enabled: true,
            active: false
        },
        InputMark: {
            type: DrawToolTypes.INPUTMARK,
            enabled: true,
            active: false
        },
        Buffer: {
            type: DrawToolTypes.BUFFER,
            enabled: false,
            active: false
        },
        Remove: {
            type: DrawToolTypes.REMOVE,
            enabled: false,
            active: false
        },
        Clean: {
            type: DrawToolTypes.CLEAN,
            enabled: true,
            active: false
        }
    },
    measureAvailable: true,
    mutex: [
        DrawToolTypes.SELECT,
        DrawToolTypes.POINT,
        DrawToolTypes.LINESTRING,
        DrawToolTypes.CIRCLE,
        DrawToolTypes.RECTANGLE,
        DrawToolTypes.POLYGON
    ],
    buffer: {
        unit: 'kilometers',
        value: 0,
        clone: false
    },
    inputMark: null
};

export function drawToolReducer(
    state: DrawToolState = initialDrawToolState,
    action: DrawToolAction
): DrawToolState {
    switch(action.type) {
        case DrawToolActionTypes.DRAW_TOOL_ACTIVE: {
            const { [action.payload]: selected } = state.entities;
            const active = { ...selected, active: true };
            let deactives = {};
            if(state.mutex.includes(action.payload)) {
                deactives = state.mutex.reduce((data, key) => {
                    if(key !== action.payload) {
                        data[key] = { ...state.entities[key], active: false };
                    }
                    return data;
                }, deactives);
            }
            const entities = { ...state.entities, ...deactives, [action.payload]: active };
            return {
                ...state,
                entities
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_DEACTIVE: {
            const { [action.payload]: selected } = state.entities;
            const deactive = { ...selected, active: false };
            const entities = {
                ...state.entities,
                [action.payload]: deactive
            };
            return {
                ...state,
                entities
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_DEACTIVE_ALL: {
            const entities = Object.keys(state.entities).reduce((data, key) => {
                data[key] = { ...state.entities[key], active: false };
                return data;
            }, {});
            return {
                ...state,
                entities
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_DISABLED: {
            const { [action.payload]: selected } = state.entities;
            const disabled = { ...selected, enabled: false, active: false };
            const entities = {
                ...state.entities,
                [action.payload]: disabled
            };
            return {
                ...state,
                entities
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_ENABLED: {
            const { [action.payload]: selected } = state.entities;
            const enabled = { ...selected, enabled: true, active: false };
            const entities = {
                ...state.entities,
                [action.payload]: enabled
            };
            return {
                ...state,
                entities
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_MEASURE_AVAILABLE: {
            return {
                ...state,
                measureAvailable: action.payload
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_BUFFER: {
            return {
                ...state,
                buffer: action.payload
            }
        }
        case DrawToolActionTypes.DRAW_TOOL_INPUT_MARK: {
            return {
                ...state,
                inputMark: action.payload
            };
        }
        default: {
            return state;
        }
    }
}